
function printLabel(labelled: {label:string}){
    document.write(`<p>printLabel: ${labelled.label}</p>`);
    console.log(labelled.label);
}
let myLabelObj = {label: "MyObjLabel", size: 10};
printLabel(myLabelObj);

interface LabelledVal{
    label: string;
}

function printLabel2(labelled: LabelledVal){
    console.log(labelled.label);
}

//---------------------可选接口属性------------------
interface SquareConfig{
    color?: string;
    width?: number;
}
function createSquare(config: SquareConfig):{color: string; area: number}{
    let newSquare = {color:"white", area:100};
    if(config.color)
        newSquare.color = config.color;
    if(config.width)
        newSquare.area = config.width*config.width;
    return newSquare;
}

//--------------------只读属性-------------------
interface Point{
    readonly x: number;
    readonly y: number;
}
let p1: Point = {x:1,y:2};
//p1.x = 2;   // Error

let initarray: number[] = [1,2,3,4,];
let roay: ReadonlyArray<number> = initarray;
//roay[0] = 30;
//roay.

//------------------接口定义函数类型----------------------
interface SearchFunc{
    (source: string, substring: string):boolean;
}
let mySearch: SearchFunc;
mySearch = function(src: string, sub: string):boolean{
    let result = src.search(sub);
    if(result == -1)
        return false;
    else
        return true;
}

//---------------------可索引的类型--------------------
interface StringArray{
    [index: number]: string;
}
let myArray: StringArray;
myArray = ["Bob", "Fred"];
let myName = myArray[0];
console.log(`myName: ${myName}`);

class Animal{
    name: string;
}
class Dog extends Animal{
    breed: string;
}
interface NotOkay{
    // [x: number]: Animal; // 没明白为什么不行？
    [x: string]: Dog;
}

interface NumberDictionary{
    [index: string]: number;
    //[index: string]: string;    // 索引类型不能相同
    //[index: number]: string;    // 为什么不行？？
    length: number;
    //name: string;
}

//--------------------类类型，实现接口-------------------
interface ClodkInterface{
    currentTime: Date;
}

class Clock implements ClodkInterface{
    currentTime: Date;  // 实类里必须要有接口的对象
    constructor(h: number, m: number){}
}

interface ClockInterface2{
    currentTime: Date;
    setTime(d: Date);
}
class Clock2 implements ClockInterface2{
    currentTime: Date;
    setTime(d: Date){
        this.currentTime = d;
    }
    constructor(h: number, m: number){

    }
}

//---------------类静态部分与实例部分的区别-------------------





//--------------扩展接口--------------------------
interface Shape{
    color: string;
}
interface Square extends Shape{
    sideLength: number;
}
let square = <Square>{};
square.color = "bule";
square.sideLength = 20;

interface Shape2{
    color: string;
}
interface PenStroke{
    penWidth: number;
}
interface Square extends Shape, PenStroke{
    sideLength: number;
}
let square2 = <Square>{};
square2.color = "blue";
square2.penWidth = 2.0;
square2.sideLength = 20;

// 混合类型
interface Counter{
    (start: number):string;
    interval: number;
    reset(): void;
}
function getCounter(): Counter{
    let counter = <Counter>function(start: number){};
    counter.interval = 120;
    counter.reset = function(){};
    return counter;
}
let ct = getCounter();
ct(10);
ct.reset();
ct.interval = 1.2;
// 在使用JavaScript第三方库的时候，你可能需要像上面那样去完整地定义类型。

// 接口继承类，这个接口只能被这个类或其子类所实现(implements)
class Control{
    private state: any;
}
interface SelectableControl extends Control{
    slect(): void;
}
interface Button extends Control{
    select(): void;
}
























